package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.Building;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.DeclareStatus;
import cn.yuhh.core.type.Role;
import cn.yuhh.core.type.Status;
import com.magic.spring.mongo.IMongoUpdate;
import com.magic.spring.mongo.Mongo;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-18 15:36
 * @descreption 提交申报
 **/
@Component("declare1.0")
@NotNull(name = {"buildingName"})
public class DeclareService extends AbstractApiService {
    @Override
    public Map service(Map request) {
        String buildingId = string(request, "buildingId");
        String buildingName = string(request, "buildingName");
        String id = id(request);
        User manager = Mongo.buildMongo().eq("areaInfo.countyCode", areaInfo(request)).eq("role", Role.RECEIVER).eq("status", Status.ACTIVE).findOne(User.class);
        DeclareInfo declareInfo = map2bean(DeclareInfo.class,request);
        declareInfo.setUserId(string(request,"selfId"));
        Building dbBuilding = Mongo.buildMongo().eq("name",buildingName).findOne(Building.class);
        if(StringUtils.isEmpty(buildingId) && dbBuilding != null){
            declareInfo.setBuildingId(dbBuilding.getId());
        }

        if(StringUtils.isEmpty(buildingId) && dbBuilding == null){
            Building building = map2bean(Building.class, request);
            building.setName(buildingName);
            Mongo.buildMongo().insert(building);
            declareInfo.setBuildingId(building.getId());
        }
        declareInfo.setMainManagerId(manager.getId());

        if(StringUtils.isEmpty(id)){
            Mongo.buildMongo().insert(declareInfo);
        }else {
            Mongo.buildMongo().eq("_id",id).updateFirst(DeclareInfo.class, new IMongoUpdate() {
                @Override
                public void update(Update update) {
                    update.set("placeName",declareInfo.getPlaceName());
                    update.set("addr",declareInfo.getPlaceName());
                    update.set("declareStatus", DeclareStatus.accepted);
                    update.set("buildingId",declareInfo.getBuildingId());
                    update.set("buildingName",declareInfo.getBuildingName());
                    update.set("area",declareInfo.getArea());
                    update.set("areaInfo",declareInfo.getAreaInfo());
                    update.set("usageType",declareInfo.getUsageType());
                    update.set("header",declareInfo.getHeader());
                    update.set("headerTel",declareInfo.getHeaderTel());
                    update.set("contacts",declareInfo.getContacts());
                    update.set("contactsTel",declareInfo.getContactsTel());
                    update.set("placeDesc",declareInfo.getPlaceDesc());
                    update.set("businessLicense",declareInfo.getBusinessLicense());
                    update.set("plan",declareInfo.getPlan());
                    update.set("photos",declareInfo.getPhotos());
                    update.set("updateAt", System.currentTimeMillis() / 1000);
                }
            });
        }
        declareLog(declareInfo);
        return ApiResponse.success();
    }
}